let submitTokenExclude = ['/login']; //token 白名单
let ajaxCacheFlag = true;

function wdAjax(options) {
	if (!options.submitToken && submitTokenExclude.indexOf(options.url) == -1) {
		SubmitToken(function (submitToken) {
			wdAjax($.extend(options, {
				submitToken: submitToken || ''
			}));
		})
		return false;
	}
	if (options.data && typeof options.data === "object" && options.type != 'get' && options.type != 'GET') {
		options.data = JSON.stringify(options.data);
	}

	let ajaxOptions = {
		url: options.testUrl || options.url,
		type: options.type || "POST",
		data: options.data,
		headers: {
			"submit_token": options.submitToken || ''
		},
		dataType: "json",
		contentType: options.contentType || "application/json",
		success: function (data, textStatus) {
			if (data.code && data.code == 'O000000') {
				if (options.success) {
					if (options.ajaxCatch === true) {
						let key = getHash(options.url, options.type, options.data);
						try {
							store.set(key, data);
						} catch (e) {
							//缓存失败，清除所有ajax缓存
							let obj = store.getAll();
							for (let key in obj) {
								if (key.indexOf('ajaxCatchKey') == 0) {
									store.remove(key)
								}
							}
						}
					}

					options.success(data.result);
				}
			} else {
				alertify.error(data.message);
			}
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			if (isDebug && isDebug == true && options.testUrl) {
				options.testUrl = '';
				wdAjax(options);
				return false;
			}
			if (options.error) {
				options.error(XMLHttpRequest);
			}
			console.error(XMLHttpRequest.status);
			console.error(XMLHttpRequest.readyState);
			console.error(textStatus);


			if (XMLHttpRequest.responseJSON) {
				if (XMLHttpRequest.responseJSON.resultCode) {
					//系统异常
				} else {
					//校验异常
					let html = '';
					if (XMLHttpRequest.responseJSON.message == '参数验证失败') {
						XMLHttpRequest.responseJSON.result.forEach(function (msg) {
							html += '<li>' + msg + '</li>'
						});
					} else {
						html += '<li>' + XMLHttpRequest.responseJSON.message + '</li>'
					}
					alertify.error('<div class="wd-alert-error-container"><ul>' + html + '</ul></div>');
				}
			} else {
				//未捕获异常
			}
		},
		complete: function (XMLHttpRequest, textStatus) {
			if (options.complete) {
				options.complete();
			}
		}
	};

	if (mock[ajaxOptions.url.replace(/^\//, '').replace(/\?.*/, '')]) {
		options.success(mock[ajaxOptions.url.replace(/^\//, '').replace(/\?.*/, '')].result);
		return false;
	}
	if (!ajaxCatch(ajaxOptions, options)) {
		//直接ajax请求数据，并处理业务逻辑
		$.ajax(ajaxOptions);
	} else {
		//用缓存数据,再调用ajax请求更新缓存
		setTimeout(function () {
			let key = getHash(ajaxOptions.url, ajaxOptions.type, ajaxOptions.data);
			options.success(store.get(key).result);
			$.ajax($.extend(ajaxOptions, {
				success: function (data, textStatus) {
					if (data.code && data.code == 'O000000') {
						if (options.success) {
							let key = getHash(options.url, options.type, options.data);
							try {
								store.set(key, data);
							} catch (e) {
								//缓存失败，清除所有ajax缓存
								let obj = store.getAll();
								for (let key in obj) {
									if (key.indexOf('ajaxCatchKey') == 0) {
										store.remove(key)
									}
								}
							}
						}
					}
				}
			}));
		})
	}
};

function ajaxCatch(ajaxOptions, options) {
	let key = getHash(ajaxOptions.url, ajaxOptions.type, ajaxOptions.data);
	if (ajaxCacheFlag && store.get(key) && options.ajaxCatch === true) {
		return true;
	} else {
		return false;
	}
}
function getHash(url, type, data) {
	return 'ajaxCatchKey-' + fcUtil.hash(url + type + JSON.stringify(data));
}
function SubmitToken(callback) {
	callback({})
	// $.ajax({
	// 	url: "/submittoken/get",
	// 	type: 'GET',
	// 	dataType: "json",
	// 	success: function (data) {
	// 		if (data.code == 'O000000') {
	// 			callback(data.result)
	// 		} else {
	// 			console.error('获取submitToken异常!')
	// 		}
	// 	},
	// 	error: function (data) {
	// 		console.error('获取submitToken异常!')
	// 	}
	// });
}
